package com.wn32.main.demo7;

public class Shop {
    private static int sometingNum=1;
    //静态obj
    public static Object obj=new Object();
    //加锁使线程安全
    public void buyOne(){
        //等价于把synchronized放在方法上 锁对象而不是锁方法
//        synchronized (this) {
        synchronized (obj){
            if (sometingNum > 0) {
                System.out.println(Thread.currentThread().getName() + ":成功购买一件");
                sometingNum--;
            } else {
                System.out.println(Thread.currentThread().getName() + ":库存不足 购买失败!");
            }
        }
    }

}
